#include "dataconfig.h"
#include "playscene.h"

#include <QPainter>
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QMessageBox>
#include <QLabel>
#include <QArgument>
#include <QFont>
#include <mypushbutton.h>
#include <qDebug>
#include <QPropertyAnimation>
#include <QSound>
PlayScene::PlayScene(QWidget *parent) : QMainWindow(parent)
{

}

PlayScene::PlayScene(int num)
{
    level = num;

    // 设置尺寸大小
    this->setFixedSize(320,588);
    // 设置图标
    this->setWindowIcon(QIcon(":/img/res/Coin0001.png"));
    // 设置标题
    this->setWindowTitle(QString("第" + QString::number(num) + "关"));
    // 设置菜单栏
    QMenuBar *bar = menuBar();
    setMenuBar(bar);
    // 添加菜单
    QMenu * menu = bar->addMenu("开始");
    // 添加行为
    QAction * actionQuit = menu->addAction("离开");

    connect(actionQuit,&QAction::triggered,[=](){
        QMessageBox::StandardButton flag = QMessageBox::question(this,"是否退出","真的要退出吗？",
                                                                 QMessageBox::Yes | QMessageBox::No);
        if(flag == QMessageBox::Yes)
            this->close();
    });

    // 左下角的文本提示第几关
    QLabel * label = new QLabel;
    QString str = QString("Level:%1").arg(level);
    label->setText(str);
    label->setParent(this);
    QFont font;
    font.setFamily("宋体");
    font.setPointSize(20);

    label->setFont(font);
    // 一口气设置大小和位置
    label->setGeometry(30,height()-50,120,50);
    // 返回音效
    QSound *backSound = new QSound(":/img/res/BackButtonSound.wav",this);
    //翻金币音效
    QSound *flipSound = new QSound(":img/res/ConFlipSound.wav",this);
    //胜利按钮音效
    QSound *winSound = new QSound(":img/res/LevelWinSound.wav",this);

    // 设置返回按钮
    MyPushButton * btn_back = new MyPushButton(":/img/res/BackButton.png",
                                               ":/img/res/BackButtonSelected.png");
    btn_back->setParent(this);
    // 设置位置
    btn_back->move(this->width()-btn_back->width(),this->height()-btn_back->height());
    connect(btn_back,&QPushButton::clicked,[=](){
        backSound->play();
        emit this->chooseSceneBack();
    });

    // 创建胜利图片
    QLabel * winLabel = new QLabel;
    QPixmap pixmap;
    pixmap.load(":/img/res/LevelCompletedDialogBg.png");
    winLabel->setPixmap(pixmap);
    winLabel->setGeometry(0,0,pixmap.width(),pixmap.height());
    winLabel->setParent(this);
    winLabel->move((this->width()-pixmap.width())*0.5 ,-pixmap.height());

    // 对地图进行编辑
    dataConfig config;
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            array[i][j] = config.mData[level][i][j];
        }
    }
    // 创建金币背景
    for(int i = 0;i<4;i++){
        for(int j=0;j<4;j++){
            QLabel *label = new QLabel;
            label->setPixmap(QPixmap(":/img/res/BoardNode.png"));
            label->setParent(this);
            label->setGeometry(0,0,50,50);
            label->move(57 + i*50,200+j*50);
            QString img = ":/img/res/Coin0008.png";
            if(array[i][j] == 1)
                img = ":/img/res/Coin0001.png";

            // 创建金币
            MyCoin * coin = new MyCoin(img,i,j,array[i][j]);
            coin->setParent(this);
            coin->move(59 + i*50,204+j*50);
            coins[i][j] = coin;
            connect(this,&PlayScene::callAllCoinIsWin,coin,&MyCoin::setWin);
            connect(coin,&MyCoin::clicked,this,[=](){
                flipSound->play();
                // 将所有金币都设置不可点击
                for(int i=0;i<4;i++){
                    for(int j=0;j<4;j++)
                        coins[i][j]->setWin(true);
                }
                array[i][j] = !array[i][j];
                qDebug() << i << j;
                // 翻转起来的硬币
                QTimer::singleShot(300,this,[=](){
                    int x = coin->getX();
                    int y = coin->getY();
                    if(x+1 <=3)
                    {
                        qDebug() << x+1 << y;
                        coins[x+1][y]->changeFlag();
                        array[x+1][y] = !array[x+1][y];
                    }
                    if(x-1>=0)
                    {
                        qDebug() << x-1 << y;
                        coins[x-1][y]->changeFlag();
                        array[x-1][y] = !array[x-1][y];
                    }
                    if(y+1<=3)
                    {
                        qDebug() << x << y+1;
                        coins[x][y+1]->changeFlag();
                        array[x][y+1] = !array[x][y+1];
                    }
                    if(y-1>=0)
                    {
                        qDebug() << x << y-1;
                        coins[x][y-1]->changeFlag();
                        array[x][y-1] = array[x][y-1];
                    }
                    // 判断是否胜利
                    if(isWin()){
                        qDebug() << "胜利了";
                        emit callAllCoinIsWin(true); // 胜利了告诉所有硬币
                        QPropertyAnimation *animation = new QPropertyAnimation(winLabel,"geometry");
                        // 设置动画间隔
                        animation->setDuration(1000);
                        // 设置动画开始位置
                        animation->setStartValue(QRect(winLabel->x(),winLabel->y(),
                                                       winLabel->width(),winLabel->height()));
                        // 设置动画结束位置
                        animation->setEndValue(QRect(winLabel->x(),winLabel->y() + 114,
                                                       winLabel->width(),winLabel->height()));
                        // 设置动画的播放方式 线性 非线性等
                        animation->setEasingCurve(QEasingCurve::OutBounce);
                        // 启动动画
                        animation->start();
                        winSound->play();
                    }
                    // 将所有金币都设置不可点击
                    for(int i=0;i<4;i++){
                        for(int j=0;j<4;j++)
                            coins[i][j]->setWin(false);
                    }
                });
            });
        }
    }

}

void PlayScene::paintEvent(QPaintEvent *)
{
    // 画者
    QPainter painter(this);
    QPixmap pixmap;
    pixmap.load(":/img/res/PlayLevelSceneBg.png");
    // 绘画
    painter.drawPixmap(0,0,this->width(),this->height(),pixmap);
    // 标题
    pixmap.load(":/img/res/Title.png");
    pixmap = pixmap.scaled(pixmap.width()*0.5,pixmap.height()*0.5);
    painter.drawPixmap( 10,30,pixmap.width(),pixmap.height(),pixmap);


}

bool PlayScene::isWin()
{
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            if(!coins[i][j]->getFlag())
                return false;
        }
    }
    return true;
}
